package com.ydj.framework.config;

import com.ydj.BaseConst;
import com.ydj.framework.filter.CrossFilter;
import com.ydj.framework.filter.HttpServletRequestWrapperFilter;
import com.ydj.framework.resolver.ExtArgumentResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 过滤器配置
 */
@Configuration
public class FilterConfig {
    @Autowired
    private List<BaseConst> constList;

    private String[] patternArray;
    @PostConstruct
    public void init() {
        // 各个子项目的路径
        List<String> patternList = constList.stream()
                .map(item-> item.getBaseUrl() + "/*")
                .collect(Collectors.toList());
        patternList.add("/common/*");
        patternList.add("/debug/*");
        patternArray = patternList.toArray(new String[0]);
    }

    @Bean
    public FilterRegistrationBean logFilter() {
        FilterRegistrationBean<HttpServletRequestWrapperFilter> registration = new FilterRegistrationBean<>();
        registration.setFilter(new HttpServletRequestWrapperFilter());
        registration.addUrlPatterns(patternArray);
        registration.setName("LogFilter");
        registration.addInitParameter("exclusions", "/static/*");
        registration.addInitParameter("exclusions", "/robots.txt");
        registration.addInitParameter("exclusions", "/WW_verify_*");
        registration.setOrder(2);
        return registration;
    }

    @Bean
    public FilterRegistrationBean crossFilter() {
        FilterRegistrationBean<CrossFilter> registration = new FilterRegistrationBean<>();
        registration.setFilter(new CrossFilter());
        registration.addUrlPatterns(patternArray);
        registration.setName("CrossFilter");
        registration.setOrder(1);
        return registration;
    }
}